<?php

/**
 * Description of BPermitObj
 * use to store permit information
 * @author cnjianfeng@gmail.com
 */
class BPermitObj {

    const Access_Allow = 'allow';
    const Access_deny = 'deny';
    const Scope_All = 'all_permits';
    const PermitSpaceScope_AllSpace = "allSpaces";
    const PermitSpaceScope_ActiveSpace = "activeSpace";

    public $permitName = null;
    public $decription = null;
    public $profile = null;
    protected $spaceId = null;
    protected $appId = null;
    protected $id = null;

    public function __construct($data = NULL) {
        /**
         * if $data is not null, try to load obj from database
         */
        if (!is_null($data))
            $this->load($data);
    }

    public function getId($data = NULL) {
        return $this->id;
    }

    /**
     * this function will load permit object from data store
     * @param type $data
     */
    protected function load($data = NULL) {
        /**
         * if number, treat as permit object id
         */
        if (is_numeric($data)) {
            $id = $data;

            $mPermit = BMPermit::model()->findByPk($id);

            if ($mPermit != NULL) {
                $this->id = $id;
                $this->permitName = $mPermit->permit_name;
                $this->decription = $mPermit->description;
                $this->appId = $mPermit->app_id;
                $this->spaceId = $mPermit->space_id;
                $this->profile = $mPermit->profile;
            }
        }
    }

    /**
     * save to data store
     * @param type $data
     */
    public function save($data = null) {

        if ($data != NULL) {
            $this->permitName = isset($data['permitName']) ? $data['permitName'] : $this->permitName;
            $this->decription = isset($data['description']) ? $data['description'] : $this->decription;
            $this->appId = isset($data['appId']) ? $data['appId'] : $this->appId;
            $this->spaceId = isset($data['spaceId']) ? $data['spaceId'] : $this->spaceId;
            $this->profile = isset($data['profile']) ? $data['profile'] : $this->profile;
        }
        /**
         * if not id, means it is new record for database
         */
        if ($this->id == NULL) {
            $mPermit = new BMPermit();
        } else {
            $mPermit = BMPermit::model()->findByPk($this->id);
        }
        $mPermit->permit_name = $this->permitName;
        $mPermit->description = $this->decription;
        $mPermit->app_id = $this->appId;
        $mPermit->space_id = $this->spaceId;
        $mPermit->profile = $this->profile;
        $mPermit->save();
    }

    protected function getspaceId($data = NULL) {
        return $this->spaceId == NULL ? Blyn::app()->getActiveSpace()->getId() : $this->spaceId;
    }

    protected function getappId($data = NULL) {
        return $this->appId == NULL ? Blyn::app()->getActiveApp()->getId() : $this->appId;
    }

    public function setspaceId($spaceId) {
        $this->spaceId = $spaceId;
    }

    public function setappId($appId) {
        $this->appId = $appId;
    }

    /**
     * use this function to find permitObject under a App
     * @param type $data
     * @return array array of permitObjects
     */
    public static function findPermitObjects($appId = NULL, $spaceId = NULL, $data = array()) {

        $appId = $appId == NULL ? Blyn::app()->getActiveApp()->getId() : $appId;
        $spaceId = $spaceId == NULL ? Blyn::app()->getActiveApp()->getId() : $spaceId;

        $attributes = array();

        $attributes['app_id'] = $appId;
        $permitName = isset($data['permitName']) ? $data['permitName'] : NULL;
        if ($permitName != NULL)
            $attributes['permit_name'] = $permitName;

        $mPermits = BMPermit::model()->findAllByAttributes($attributes);
        $permitObjects = array();

        /**
         * if space_id == null, means it used for all Space
         */
        foreach ($mPermits as $mPermit) {
            if ($mPermit->space_id == NULL || $mPermit->space_id == $spaceId) {
                $permitObj = new BPermitObj($mPermit->_id);
                $permitObjects[$mPermit->_id] = $permitObj;
            }
        }

        return $permitObjects;
    }

    public static function findPermitObjectByName($permitName, $matchExactly = FALSE) {
        if (is_string($permitName)) {
            $permitObjects = BPermitObj::findPermitObjects(NULL, NULL, array('permitName' => $permitName));
            if (count($permitObjects) > 0) {
                if ($matchExactly) {
                    if (count($permitObjects) == 1)
                        return $permitObjects[0];
                }else {
                    return $permitObjects[0];
                }
            }
        }
        return FALSE;
    }

}

?>
